package cn.stronglink.assetmanage.business.task.view.sync;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.task.adapter.TaskListAdapter;
import cn.stronglink.assetmanage.business.task.model.TaskSyncModel;
import cn.stronglink.assetmanage.business.task.model.TaskWaitModel;
import cn.stronglink.assetmanage.business.task.presenter.TaskSyncPresenter;
import cn.stronglink.assetmanage.business.task.presenter.TaskWaitPresenter;
import cn.stronglink.assetmanage.business.task.view.wait.TaskWaitView;
import cn.stronglink.assetmanage.index.view.onRecyclerListener;
import cn.stronglink.assetmanage.widget.ViewPagerFragment;

/**
 *待同步任务
 */
public class TaskSyncFragment extends ViewPagerFragment {
    private LinearLayoutManager layoutManager;
    private RecyclerView rv_tasksync_order_list;
    private TaskListAdapter listAdapter;
    private TaskSyncModel taskSyncModel;
    private TaskSyncView taskSyncView;
    private TaskSyncPresenter taskSyncPresenter;
    private Button btn_task_sync;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_task_sync,container,false);
        initViewAndData(rootView);
        return rootView;
    }

    private void initViewAndData(View view) {
        rv_tasksync_order_list = view.findViewById(R.id.rv_tasksync_order_list);
        btn_task_sync = view.findViewById(R.id.btn_task_sync);
        btn_task_sync.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                taskSyncPresenter.sync();
            }
        });
        listAdapter = new TaskListAdapter(getActivity());
        layoutManager = new LinearLayoutManager(this.getActivity(), LinearLayout.VERTICAL,false);
        rv_tasksync_order_list.setLayoutManager(layoutManager);
        rv_tasksync_order_list.setAdapter(listAdapter);
        listAdapter.setOnItemClickListener(new onRecyclerListener() {
            @Override
            public void onItemClicked(View v, int postion) {
                taskSyncView.showTaskAssetFragment(postion);
            }
        });
        taskSyncModel = new TaskSyncModel();
        taskSyncView = new TaskSyncView(this,view,listAdapter);
        taskSyncPresenter = new TaskSyncPresenter(taskSyncModel,taskSyncView);
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        try{
            if(isVisible && taskSyncPresenter!=null){
                taskSyncPresenter.initWaitTaskData();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

}
